<template>
  <UploadExcel :on-success="onSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    async  onSuccess(data) {
      const zidian = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }

      console.log(data.results)
      const newData = data.results.map(user => {
        const newUser = {}
        for (const key in user) {
          // 遍历的key 手机号 姓名 日期
          // console.log(21, key)
          const enKey = zidian[key] // ['姓名']===username
          // console.log(23, Key)
          const value = user[key] // ['姓名']===王小山
          // newUser[Key] = value // username: 王小山
          if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
            newUser[enKey] = new Date(this.formatExcelTime(value))
          } else {
            newUser[enKey] = value // { username: '王小山' }
          }
        }
        return newUser
      })
      console.log('翻译后的用户数组', newData)
      // 发起请求
      await importEmployee(newData)
      this.$message.success('导入成功')
      this.$router.back()
    },
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
